package com.vercon.common.manager;

import android.content.Context;
import android.media.AudioManager;

import androidx.annotation.IntDef;

import com.vercon.common.utils.LogUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by csh on 2021/1/25
 * 媒体音量控制,可修改音量控制类型
 */
public class AudioMngHelper {

    private final String TAG = getClass().getSimpleName();
    private final boolean OpenLog = true;

    private AudioManager audioManager;
    private int NOW_AUDIO_TYPE = TYPE_MUSIC;
    private int NOW_FLAG = FLAG_NOTHING;
    private int VOICE_STEP = 1; //0-MAX_VALUE的步进。
    private int MAX_VALUE;//最大音量值，默认是系统最大音量 * 10

    /**
     * 封装：STREAM_类型
     */
    public final static int TYPE_MUSIC = AudioManager.STREAM_MUSIC;
    public final static int TYPE_ALARM = AudioManager.STREAM_ALARM;
    public final static int TYPE_RING = AudioManager.STREAM_RING;

    @IntDef({TYPE_MUSIC, TYPE_ALARM, TYPE_RING})
    @Retention(RetentionPolicy.SOURCE)
    public @interface TYPE {

    }

    /**
     * 封装：FLAG
     */
    public final static int FLAG_SHOW_UI = AudioManager.FLAG_SHOW_UI;
    public final static int FLAG_PLAY_SOUND = AudioManager.FLAG_PLAY_SOUND;
    public final static int FLAG_NOTHING = 0;

    @IntDef({FLAG_SHOW_UI, FLAG_PLAY_SOUND, FLAG_NOTHING})
    @Retention(RetentionPolicy.SOURCE)
    public @interface FLAG {
    }

    /**
     * 初始化，获取音量管理者
     *
     * @param context 上下文
     */
    public AudioMngHelper(Context context) {
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        MAX_VALUE = audioManager.getStreamMaxVolume(NOW_AUDIO_TYPE) * 10;
    }

    //获取最大音量
    public int getSystemMaxVolume() {
        return MAX_VALUE;
    }

    //获取当前音量
    public int getSystemCurrentVolume() {
        return audioManager.getStreamVolume(NOW_AUDIO_TYPE);
    }

    /**
     * @return 获取当前的音量值
     */
    public int getCurrentVolume() {
        return getSystemCurrentVolume() * 10;
    }

    /**
     * 修改步进值
     *
     * @param step step
     * @return this
     */
    public AudioMngHelper setVoiceStep(int step) {
        VOICE_STEP = step;
        return this;
    }


    /**
     * 改变当前的模式，对全局API生效
     *
     * @param type
     * @return
     */
    public AudioMngHelper setAudioType(@TYPE int type) {
        NOW_AUDIO_TYPE = type;
        return this;
    }

    /**
     * 改变当前FLAG，对全局API生效
     *
     * @param flag
     * @return
     */
    public AudioMngHelper setFlag(@FLAG int flag) {
        NOW_FLAG = flag;
        return this;
    }

    public AudioMngHelper addVoiceSystem() {
        audioManager.adjustStreamVolume(NOW_AUDIO_TYPE, AudioManager.ADJUST_RAISE, NOW_FLAG);
        return this;
    }

    public AudioMngHelper subVoiceSystem() {
        audioManager.adjustStreamVolume(NOW_AUDIO_TYPE, AudioManager.ADJUST_LOWER, NOW_FLAG);
        return this;
    }

    /**
     * 调整音量，自定义
     * * @return 改完后的音量值
     */
    public int setVoice(int a) {
        a = Math.max(a, 0);
        a = Math.min(a, MAX_VALUE);

        if (a != 0 && a / 10 <= 0) {
            a = 1;
        } else {
            a = a / 10;
        }
        LogUtils.d(a + "");
        audioManager.setStreamVolume(NOW_AUDIO_TYPE, a, 0);
        return getCurrentVolume();
    }

    /**
     * 步进加，步进值可修改
     * 0——100
     *
     * @return 改完后的音量值
     */
    public int addVoice() {
        int a = (VOICE_STEP + getCurrentVolume() / 10);
        a = Math.max(a, 0);
        a = Math.min(a, MAX_VALUE / 10);
        audioManager.setStreamVolume(NOW_AUDIO_TYPE, a, NOW_FLAG);
        return getCurrentVolume();
    }

    /**
     * 步进减，步进值可修改
     * 0——100
     *
     * @return 改完后的音量值
     */
    public int subVoice() {
        int a = getCurrentVolume() / 10 - VOICE_STEP;
        a = Math.max(a, 0);
        a = Math.min(a, MAX_VALUE / 10);
        audioManager.setStreamVolume(NOW_AUDIO_TYPE, a, NOW_FLAG);
        return getCurrentVolume();
    }

} 